package zelix.gui.clickguis.caesium.util;

import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import org.lwjgl.opengl.GL11;

import java.awt.*;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL11.glDisable;

/**
 * @author sendQueue <Vinii>
 *
 *         Further info at Vinii.de or github@vinii.de, file created at 11.11.2020. 
 *         Use is only authorized if given credit!
 * 
 */
public final class RenderUtil {


	public static void drawRect(final float x, final float y, final float width, final float height, final int color) {
		glEnable(3042);
		glDisable(3553);
		GL11.glBlendFunc(770, 771);
		glEnable(2848);
		GL11.glShadeModel(7425);

		GL11.glPushMatrix();
		GL11.glBegin(7);

		glColor(color);

		GL11.glVertex2d(x, y);
		GL11.glVertex2d(x, y + height);
		GL11.glVertex2f(x + width, y + height);
		GL11.glVertex2f(x + width, y);

		GL11.glEnd();
		GL11.glPopMatrix();

		glEnable(3553);
		glDisable(3042);
		glDisable(2848);
		GL11.glShadeModel(7424);
	}

	public static void relativeRect(final float left, final float top, final float right, final float bottom, final int color) {

		final Tessellator tessellator = Tessellator.getInstance();
		final BufferBuilder worldRenderer = tessellator.getBuffer();

		GlStateManager.enableBlend();
		GlStateManager.disableTexture2D();
		GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
		glColor(color);
		/*        worldRenderer.begin -> .func_181668_a
        worldRenderer.pos -> .func_181662_b*/
		worldRenderer.begin(7, DefaultVertexFormats.POSITION);
		worldRenderer.pos(left, bottom, 0).endVertex();
		worldRenderer.pos(right, bottom, 0).endVertex();
		worldRenderer.pos(right, top, 0).endVertex();
		worldRenderer.pos(left, top, 0).endVertex();

		tessellator.draw();

		GlStateManager.enableTexture2D();
		GlStateManager.disableBlend();
	}

	public static void drawFilledCircle(final float xx, final float yy, final float radius, final Color color) {
		int sections = 1920;
		double dAngle = 2 * Math.PI / sections;
		float x, y;
		glPushAttrib(GL_ENABLE_BIT);
		glEnable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_LINE_SMOOTH);
		glBegin(GL11.GL_TRIANGLE_FAN);
		for (int i = 0; i < sections; i++) {
			x = (float) (radius * Math.sin((i * dAngle)));
			y = (float) (radius * Math.cos((i * dAngle)));

			glColor4f(color.getRed() / 255F, color.getGreen() / 255F, color.getBlue() / 255F, color.getAlpha() / 255F);
			glVertex2f(xx + x, yy + y);
		}
		GlStateManager.color(0, 0, 0);
		glEnd();
		glPopAttrib();
	}

	public static void drawHorizontalGradient(final float x, final float y, final float width, final float height,
											  final int leftColor, final int rightColor) {
		glEnable(3042);
		glDisable(3553);
		GL11.glBlendFunc(770, 771);
		glEnable(2848);
		GL11.glShadeModel(7425);

		GL11.glPushMatrix();
		GL11.glBegin(7);

		glColor(leftColor);

		GL11.glVertex2d(x, y);
		GL11.glVertex2d(x, y + height);

		glColor(rightColor);

		GL11.glVertex2f(x + width, y + height);
		GL11.glVertex2f(x + width, y);

		GL11.glEnd();
		GL11.glPopMatrix();

		glEnable(3553);
		glDisable(3042);
		glDisable(2848);
		GL11.glShadeModel(7424);
	}


	public static void drawVerticalGradient(final float x, final float y, final float width, final float height,
											final int topColor, final int bottomColor) {
		glEnable(3042);
		glDisable(3553);
		GL11.glBlendFunc(770, 771);
		glEnable(2848);
		GL11.glShadeModel(7425);

		GL11.glPushMatrix();
		GL11.glBegin(7);

		glColor(topColor);

		GL11.glVertex2f(x, y + height);
		GL11.glVertex2f(x + width, y + height);

		glColor(bottomColor);

		GL11.glVertex2f(x + width, y);
		GL11.glVertex2f(x, y);

		GL11.glEnd();
		GL11.glPopMatrix();

		glEnable(3553);
		glDisable(3042);
		glDisable(2848);
		GL11.glShadeModel(7424);
	}

	public static void glColor(final int hex) {
		//shifting
		final float alpha = (hex >> 24 & 255) / 255f;
		final float red = (hex >> 16 & 255) / 255f;
		final float green = (hex >> 8 & 255) / 255f;
		final float blue = (hex & 255) / 255f;

		GL11.glColor4f(red, green, blue, alpha);
	}
	public static void drawRect(int left, int top, int right, int bottom, int color) {
		// default
		Gui.drawRect(left, top, right, bottom, color);

		// replacement
//		LWJGLUtil.drawRect(left, top, right - left, bottom - top, color);
	}

	public static void drawHorizontalLine(int startX, int endX, int y, int color) {
		if (endX < startX) {
			int i = startX;
			startX = endX;
			endX = i;
		}

		drawRect(startX, y, endX + 1, y + 1, color);
	}
	public static void drawVerticalLine(int x, int startY, int endY, int color) {
		if (endY < startY) {
			int i = startY;
			startY = endY;
			endY = i;
		}

		drawRect(x, startY + 1, x + 1, endY, color);
	}
	
	/**
	 * Checks if mouse is over rectangle.
	 * 
	 * @param x
	 * @param y
	 * @param width RELATIVE to x.
	 * @param height RELATIVE to y.
	 * @param mouseX
	 * @param mouseY
	 * @return true if rectangle is hovered
	 */
	public static boolean isHovered(int x, int y, int width, int height, int mouseX, int mouseY) {
		return (mouseX >= x) && (mouseX <= x + width) && (mouseY >= y) && (mouseY < y + height);
	}


}
